// 登录
import request from './request'
export default function wxLogin() {
    return new Promise((resolve, reject) => {
        wx.getUserProfile({
            desc: '授权窗口', //必选，弹出授权窗口
            success: res => { //授权成功，获取用户信息
                const userInfo = res.userInfo;
                wx.login({ //login方法获取code
                    success: res => {
                        const code = res.code;
                        // 拿到code发送登录请求
                        request({
                            url: "/user/wxLogin",
                            method: "POST",
                            data: {
                                code,
                                appId: "wx4a618012bf72964b",
                                appSecret: "f41a21ad7ee9b29f1bee0fc3b2e61b5f"
                            },
                            success: res => {
                                // 拿到登录请求返回的结果保存token，userInfo
                                if (res.statusCode == 200) {
                                    wx.showToast({
                                        title: '登录成功',
                                        icon: 'success',
                                        duration: 2000
                                    })
                                    const {token} = res.data
                                    // 保存用户数据到数据库
                                    const {
                                        avatarUrl,
                                        province,
                                        gender,
                                        nickName
                                    } = userInfo
                                    request({
                                        url: '/user/auth',
                                        method: 'POST',
                                        data: {
                                            token,
                                            avatarUrl,
                                            nickName,
                                            gender,
                                            province
                                        },
                                        header: {
                                            Authorization: token
                                        },
                                        success(res) {
                                            if (res.data) {
                                                request({
                                                    url: '/user/getUserInfo',
                                                    data: {token},
                                                    header: { 
                                                        Authorization: token
                                                    },
                                                    success(res){
                                                        const userInfo=res.data[0]
                                                        wx.setStorageSync('token', token)  // 保存token
                                                        wx.setStorageSync('userInfo', userInfo)
                                                        getApp().globalData.userInfo =userInfo;
                                                        resolve(res)
                                                    }
                                                })
                                            }
                                        }
                                    })
                                }
                            }
                        })
                    }
                })
            },
            fail: res => {
                console.log(res);
            }
        })
    })
}